﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
    ViewData["Title"] = "库存盘点管理";
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>库存盘点管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <div class="layui-card">
        <div class="layui-card-header">
            <div class="layui-row">
                <div class="layui-col-md6">
                    <h2>库存盘点管理</h2>
                </div>
                <div class="layui-col-md6" style="text-align: right;">
                    <button class="layui-btn" id="btnAdd">
                        <i class="layui-icon">&#xe654;</i> 新增盘点单
                    </button>
                    <button class="layui-btn layui-btn-danger" id="btnBatchDel">
                        <i class="layui-icon">&#xe640;</i> 批量删除
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <!-- 搜索栏 -->
            <form class="layui-form layui-form-pane" lay-filter="searchForm">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">盘点单名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="countingOrderName" placeholder="请输入盘点单名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn" lay-submit lay-filter="searchBtn">
                            <i class="layui-icon">&#xe615;</i> 搜索
                        </button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="inventoryTable" lay-filter="inventoryTable"></table>
        </div>
    </div>
    <!-- 表格操作列模板 -->
    <script type="text/html" id="tableBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script>
        layui.use(['table', 'form', 'layer'], function () {
            var table = layui.table;
            var form = layui.form;
            var layer = layui.layer;
            var $ = layui.$;
            
            // 初始化表格
            table.render({
                elem: '#inventoryTable',
                url: '/InventoryCounts/ShowInventoryCounts',
                page: true,
                where: {},
                request: {
                    pageName: 'pageIndex',
                    limitName: 'pageSize'
                },
                cols: [[
                    { type: 'checkbox', fixed: 'left' },
                    { field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left', templet: function(d){ return d.id || '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'countingOrderCode', title: '盘点单编号', width: 200, templet: function(d){ return d.countingOrderCode || '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'countingOrderName', title: '盘点单名称', width: 120, templet: function(d){ return d.countingOrderName || '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'countingType', title: '盘点类型', width: 100, templet: function(d){ return d.countingType || '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'countingDate', title: '盘点日期', width: 200, templet: function(d){ return d.countingDate ? layui.util.toDateString(d.countingDate, 'yyyy-MM-dd HH:mm:ss') : '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'countingPerson', title: '盘点人', width: 100, templet: function(d){ return d.countingPerson || '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'repositoryName', title: '仓库名称', width: 200, templet: function(d){ return d.repositoryName || '<span style="color:#bbb">暂无</span>'; }},
                    { field: 'state', title: '单据状态', width: 100, templet: function(d){ return d.state === 1 ? '已完成' : '草稿'; }},
                    { title: '操作', toolbar: '#tableBar', width: 150, fixed: 'right'}
                ]]
            });
            
            // 搜索按钮点击事件
            form.on('submit(searchBtn)', function(data){
                table.reload('inventoryTable', {
                    where: data.field
                });
                return false;
            });
            
            // 新增按钮点击事件
            $('#btnAdd').on('click', function(){
                window.location.href = '/InventoryCounts/Edit';
            });
            
            // 批量删除按钮点击事件
            $('#btnBatchDel').on('click', function(){
                var checkStatus = table.checkStatus('inventoryTable');
                if(checkStatus.data.length === 0){
                    layer.msg('请选择要删除的记录');
                    return;
                }
                var ids = checkStatus.data.map(function(item){ return item.id; });
                layer.confirm('确定要删除选中的记录吗？', function(index){
                    $.ajax({
                        url: '/InventoryCounts/DelInventoryCounts',
                        type: 'DELETE',
                        data: JSON.stringify(ids),
                        contentType: 'application/json',
                        success: function(){
                            layer.msg('删除成功');
                            table.reload('inventoryTable');
                        },
                        error: function(xhr){
                            layer.msg('删除失败：' + xhr.responseText, {icon: 2});
                        }
                    });
                    layer.close(index);
                });
            });
            
            // 表格工具条事件
            table.on('tool(inventoryTable)', function(obj){
                var data = obj.data;
                if(obj.event === 'edit'){
                    // 跳转到编辑页面
                    window.location.href = '/InventoryCounts/Edit?id=' + data.id;
                } else if(obj.event === 'del') {
                    layer.confirm('确定要删除这条盘点单吗？', function(index){
                        $.ajax({
                            url: '/InventoryCounts/DelInventoryCounts',
                            type: 'DELETE',
                            data: JSON.stringify([data.id]),
                            contentType: 'application/json',
                            success: function(){
                                layer.msg('删除成功');
                                obj.del();
                                layer.close(index);
                            },
                            error: function(xhr){
                                layer.msg('删除失败：' + xhr.responseText, {icon: 2});
                            }
                        });
                    });
                }
            });
        });
    </script>
</body>
</html>
